// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Button
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.VerticalAlignmentLine
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication
import java.io.File


fun main() = singleWindowApplication(
    title = "文件批量改名",
    resizable = false
) {

    var openFileFlag by remember { mutableStateOf(false) }
    val openFileList by remember { mutableStateOf(ArrayList<File>()) }

    var dirStr by remember { mutableStateOf("") }
    var size by remember { mutableStateOf(0) }

    var oldStr by remember { mutableStateOf("") }
    var newStr by remember { mutableStateOf("") }

    var resultCode by remember { mutableStateOf(-1) }
    var resultMsg by remember { mutableStateOf("") }

    Column {
        LazyScrollable(dirStr, size, openFileList)

        Row(
            modifier = Modifier.padding(150.dp, 0.dp, 0.dp, 0.dp),
        ) {
            Button(
                modifier = Modifier.padding(100.dp, 0.dp, 0.dp, 0.dp),
                onClick = {
                    openFileFlag = true
                    resultCode = -1
                    resultMsg = ""
                }) {
                Text("选择文件")
            }
            Button(
                modifier = Modifier.padding(100.dp, 0.dp, 0.dp, 0.dp),
                onClick = {
                    openFileList.clear()
                    size = openFileList.size
                    dirStr = ""
                }) {
                Text("清除文件")
            }


        }
        OutlinedTextField(
            value = oldStr,
            onValueChange = { oldStr = it },
            label = { Text("要被替换的内容") },
            modifier = Modifier.padding(250.dp, 0.dp, 0.dp, 0.dp),
        )
        OutlinedTextField(
            value = newStr,
            onValueChange = { newStr = it },
            label = { Text("要替换成的内容") },
            modifier = Modifier.padding(250.dp, 0.dp, 0.dp, 0.dp),
        )
        Row(
            modifier = Modifier.padding(150.dp, 0.dp, 0.dp, 0.dp),
        ) {
            Button(
                modifier = Modifier.padding(100.dp, 0.dp, 0.dp, 0.dp),
                onClick = {
                    batchModifyFileName(
                        dirStr = dirStr,
                        fileList = openFileList,
                        oldStr = oldStr,
                        newStr = newStr,
                        onResult = { code, fileNameList ->
                            resultCode = code
                            resultMsg = "以下${fileNameList.joinToString()}重命名错误"
                        }
                    )
                }) {
                Text("执行操作")
            }

            Button(
                modifier = Modifier.padding(100.dp, 0.dp, 0.dp, 0.dp),
                onClick = {
                    oldStr = ""
                    newStr = ""
                    resultMsg = ""
                    resultCode = -1
                }) {
                Text("清除输入")
            }
        }

        LazyColumn {
            item {
                Text(
                    modifier = Modifier.padding(250.dp, 0.dp, 0.dp, 0.dp),
                    text = if (resultCode == 0) "成功" else resultMsg
                )
            }
        }
    }
    if (openFileFlag) {
        FileDialog(
            title = "批量改名",
            isLoad = true,
            isMultiple = true,
            onResult = { dir, list ->
                list.forEach {
                    if (!openFileList.contains(it)) {
                        openFileList.add(it)
                    }
                }
                size = openFileList.size
                openFileFlag = false
                dirStr = dir
            }
        )
    }

}

